//function DialogObjectsCtrl ($scope, $location, $rootScope, obj, $dialogs, rest, $modalInstance) {
//    if (!$rootScope.hasOwnProperty('user')){  // 验证用户登陆
//        $location.path("/login");
//    }
//
//    $scope.obj = obj;
//
//    $scope.close = function(){
//        $modalInstance.dismiss('canceled');
//    };
//}
//
//function DialogObjCtrl ($scope, $location, $rootScope, obj, $dialogs, rest, $modalInstance) {
//    if (!$rootScope.hasOwnProperty('user')){  // 验证用户登陆
//        $location.path("/login");
//    }
//
//    $scope.obj = obj;
//    $scope.close = function(){
//        $modalInstance.dismiss('canceled');
//    };
//}
App.controller('HomeCtrl', ['$scope','$location','$rootScope','$dialogs', 'rest','$timeout', 'resolveData',
    function ($scope, $location, $rootScope, $dialogs, rest, $timeout, resolveData) {

    $scope._type = 'text';
    $scope.newContacts = $scope.newQuotes = $scope.newRequirements = $scope.newVectors = [];

    $scope.areaContactDetail = function(area){
        var dlg = $dialogs.create(
            '/static/js/views/detail/area_contact.html',
            'DialogObjectsCtrl',
            { title: area, objects: [ {id: 1, name: 'aaaaa'} ] },
            {key: false,back: 'static', windowClass: 'modal modal-large'}
        );
    };

    $scope.areaQuoteDetail = function(area){
        var dlg = $dialogs.create(
            '/static/js/views/detail/area_quote.html',
            'DialogObjectsCtrl',
            { title: area, objects: [ {id: 1, name: 'aaaaa'} ] },
            {key: false,back: 'static', windowClass: 'modal modal-large'}
        );

    };

    $scope.areaRequirementDetail = function(area){
        var dlg = $dialogs.create(
            '/static/js/views/detail/area_requirement.html',
            'DialogObjectsCtrl',
            { title: area, objects: [ {id: 1, name: 'aaaaa'} ]},
            {key: false,back: 'static', windowClass: 'modal modal-large'}
        );

    };

    $scope.areaProjectDetail = function(area){
        var dlg = $dialogs.create(
            '/static/js/views/detail/area_project.html',
            'DialogObjectsCtrl',
            { title: area, objects: [ {id: 1, name: 'aaaaa'} ] },
            {key: false,back: 'static', windowClass: 'modal modal-large'}
        );

        dlg.result.then(function(result){
            $scope.query();
        });

    };

    $scope.salesFunnelConfig = {
        chart: {
            type: 'funnel',
            marginRight: 100
        },
        title: {
            text: 'Sales funnel',
            x: -50
        },
        plotOptions: {
            series: {
                dataLabels: {
                    enabled: true,
                    format: '<b>{point.name}</b> ({point.y:,.0f})',
                    color: 'black',
                    softConnector: true
                },
                neckWidth: '30%',
                neckHeight: '25%'

                //-- Other available options
                // height: pixels or percent
                // width: pixels or percent
            }
        },
        legend: {
            enabled: false
        },
        series: [{
            name: 'Unique users',
            data: [
                ['Website visits',   15654],
                ['Downloads',       4064],
                ['Requested price list', 1987],
                ['Invoice sent',    976],
                ['Finalized',    846]
            ]
        }],
        loading: false,
        useHighStocks: false
    };

    $scope.newContactsChartConfig = {
        options: {
            chart: {
                type: 'pie'
            }
        },
        series: [{
            type: 'pie',
            name: 'Number: ',
            data: [],
            events: {
                click: function (e) {
                    //$("#test").click();
                    $timeout($scope.areaContactDetail(e.point.name), 10);
                }
            }
        }],
        tooltip: {
            pointFormat: '{series.name}: <b>{point.percentage:.1f}%</b>'
        },
        plotOptions: {
            pie: {
                allowPointSelect: true,
                cursor: 'pointer',
                dataLabels: {
                    enabled: true,
                    color: '#000000',
                    connectorColor: '#000000',
                    formatter: function() {
                        return '<b>'+ this.point.name +'</b>: '+ this.percentage +' %';
                    }
                },
                events: {
                    click: function (e) {
                        //alert(e.point.name); //弹出提示
                    }

                }
            }
        },
        title: {
            text: 'New Contacts'
        },
        loading: false,
        useHighStocks: false
    };

    $scope.newQuoteChartConfig = {
        options: {
            chart: {
                type: 'pie'
            }
        },
        series: [{
            type: 'pie',
            name: 'Number: ',
            data: [['Ordered', 10], ['Other', 15]],
            events: {
                click: function (e) {
                    $timeout($scope.areaQuoteDetail(e.point.name), 10);
                }
            }
        }],
        tooltip: {
            pointFormat: '{series.name}: <b>{point.percentage:.1f}%</b>'
        },
        plotOptions: {
            pie: {
                allowPointSelect: true,
                cursor: 'pointer',
                dataLabels: {
                    enabled: true,
                    color: '#000000',
                    connectorColor: '#000000',
                    formatter: function() {
                        return '<b>'+ this.point.name +'</b>: '+ this.percentage +' %';
                    }
                }
            }
        },
        title: {
            text: 'New Quotes'
        },
        loading: false,
        useHighStocks: false
    };

    $scope.newRequirementChartConfig = {
        options: {
            chart: {
                type: 'pie'
            }
        },
        series: [{
            type: 'pie',
            name: 'Number: ',
            data: [['Replied', 10], ['Other', 15]],
            events: {
                click: function (e) {
                    $timeout($scope.areaRequirementDetail(e.point.name), 10);
                }
            }
        }],
        tooltip: {
            pointFormat: '{series.name}: <b>{point.percentage:.1f}%</b>'
        },
        plotOptions: {
            pie: {
                allowPointSelect: true,
                cursor: 'pointer',
                dataLabels: {
                    enabled: true,
                    color: '#000000',
                    connectorColor: '#000000',
                    formatter: function() {
                        return '<b>'+ this.point.name +'</b>: '+ this.percentage +' %';
                    }
                }
            }
        },
        title: {
            text: 'New Requirements'
        },
        loading: false,
        useHighStocks: false
    };

    $scope.newVectorChartConfig = {
        options: {
            chart: {
                type: 'pie'
            }
        },
        series: [{
            type: 'pie',
            name: 'Number: ',
            data: [['Standard', 10], ['Non-standard', 15]],
            events: {
                click: function (e) {
                    $timeout($scope.areaProjectDetail(e.point.name), 10);
                }
            }
        }],
        tooltip: {
            pointFormat: '{series.name}: <b>{point.percentage:.1f}%</b>'
        },
        plotOptions: {
            pie: {
                allowPointSelect: true,
                cursor: 'pointer',
                dataLabels: {
                    enabled: true,
                    color: '#000000',
                    connectorColor: '#000000',
                    formatter: function() {
                        return '<b>'+ this.point.name +'</b>: '+ this.percentage +' %';
                    }
                }
            }
        },
        title: {
            text: 'New Vectors'
        },
        loading: false,
        useHighStocks: false
    };

    $scope.summaryData = function(resolveData){
        $scope.newContactsChartConfig.series[0].data = $scope.newContacts = resolveData.obj.chartData.newContacts;
        $scope.newQuoteChartConfig.series[0].data = $scope.newQuotes = resolveData.obj.chartData.newQuotes;
        $scope.newRequirementChartConfig.series[0].data = $scope.newRequirements = resolveData.obj.chartData.newRequirements;
        $scope.newVectorChartConfig.series[0].data = $scope.newVectors = resolveData.obj.chartData.newVectors;
    };
    $scope.summaryData(resolveData);


//    rest.query('other/summary').then(function(data) {
//        $scope.newContactsChartConfig.series[0].data = $scope.newContacts = data.obj.chartData.newContacts;
//        $scope.newQuoteChartConfig.series[0].data = $scope.newQuotes = data.obj.chartData.newQuotes;
//        $scope.newRequirementChartConfig.series[0].data = $scope.newRequirements = data.obj.chartData.newRequirements;
//        $scope.newVectorChartConfig.series[0].data = $scope.newVectors = data.obj.chartData.newVectors;
//    },function(data) {
//        $rootScope.errMsg.msg = data.data.msg || data.data.error_message || cfg.defaultErrMsg;
//    });

}])


//function LoginCtrl ($scope, $location, $http) {
//
//    $scope.login = function(){
//        $http({
//            url: '/api/v1/user/login/',
//            method: "POST",
//            data: {username: $scope.username, password: $scope.password}
//        }).success(function (data, status, headers, config) {
//                window.location = '/';
//                //$scope.persons = data; // assign  $scope.persons here as promise is resolved here
//            }).error(function (data, status, headers, config) {
//                $scope.error = status;
//            });
//    }
//}

function LogoutCtrl ($scope, $location, $http) {
    $scope.logout = function(){
        return $http.get('/api/v1/user/logout').then(function(response) {
            //return response.data.objects;
            window.location.reload();
//            window.location.href = '#!/login';
        });
    }
}
//
//function RegisterCtrl ($scope, $location, $http) {
//    $scope.register = function(){
//        $http({
//            url: '/api/v1/user/register/',
//            method: "POST",
//            data: {username: $scope.username, password: $scope.password, email: $scope.email}
//        }).success(function (data, status, headers, config) {
//                window.location = '/';
//                //$scope.persons = data; // assign  $scope.persons here as promise is resolved here
//            }).error(function (data, status, headers, config) {
//                //$scope.status = status;
//            });
//    }
//}
//
//
//function DialogsAlertCtrl($scope,$modalInstance,header,msg){
//    //-- Variables -----//
//		$scope.header = (angular.isDefined(header)) ? header : 'Notification';
//		$scope.msg = (angular.isDefined(msg)) ? msg : 'Unknown application notification.';
//		//-- Methods -----//
//		$scope.close = function(){
//			$modalInstance.close();
//			$scope.$destroy();
//		}; // end close
//};
